// FlattenTree.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stack>
using namespace std;

//Definition for binary tree
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    void flatten(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(root == NULL)
            return ;
        
        stack<TreeNode*> S;
        S.push(root);
        TreeNode np(0);  
        TreeNode* pLast = &np;  
        
        while(!S.empty())
        {
            
            TreeNode* p =S.top();
            S.pop();
            if(p==NULL)
                continue;
            
            S.push(p->right);
            S.push(p->left);
         
            pLast->right = p;
            pLast = p;
            pLast->left = NULL;
        }
        
        
    }
    

};

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

